package cn.edu.fzu.homemaking.sso.controller;

import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.exception.ServiceException;
import cn.edu.fzu.homemaking.sso.domain.entity.CompanyUser;
import cn.edu.fzu.homemaking.sso.domain.entity.Customer;
import cn.edu.fzu.homemaking.sso.domain.entity.SysUser;
import cn.edu.fzu.homemaking.sso.domain.entity.User;
import cn.edu.fzu.homemaking.sso.domain.enums.CompanyUserTypeEnum;
import cn.edu.fzu.homemaking.sso.domain.enums.SexEnum;
import cn.edu.fzu.homemaking.sso.domain.enums.StatusEnum;
import cn.edu.fzu.homemaking.sso.domain.vo.UserStatistic;
import cn.edu.fzu.homemaking.sso.service.UserService;
import cn.edu.fzu.homemaking.web.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "SSO-用户管理模块")
@RestController
@RequestMapping("/api/sso/auth/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "获取实时个人账号信息接口")
    @GetMapping("/info")
    public Result getUserInfo(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        return new Result(userService.getUserByEmail(user.getEmail(),
                (String) request.getSession().getAttribute(SessionConstant.USER_TYPE)));
    }


    @ApiOperation(value = "通过用户id查询顾客名称")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户id"),
            @ApiImplicitParam(name = "userType", value = "用户类型：USER,COMPANY") })
    @GetMapping("/getNameById")
    public Result getNameById(Long id, String userType) {
        return new Result(userService.getNameById(id, userType));
    }


    @ApiOperation(value = "个人修改基本信息")
    @ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "手机号"),
            @ApiImplicitParam(name = "address", value = "家庭地址"),
            @ApiImplicitParam(name = "faceUrl", value = "人脸照片"),
            @ApiImplicitParam(name = "email",value = "电子邮件") })
    @PostMapping("/updateInfo")
    public Result updateInfo(String email, String phone, String address, String faceUrl, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        userService.updateUser(user, email, null, phone, address, faceUrl, null);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "更新顾客状态接口-系统端")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "用户id"),
            @ApiImplicitParam(name = "status", required = true, value = "状态") })
    @PostMapping("/updateCustomerStatus")
    public Result updateCustomerStatus(@RequestParam Long id, @RequestParam StatusEnum status,
                                       HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        userService.updateCustomer(id, null, null, null, null, status, user);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "更新系统人员信息接口-系统端")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "用户id"),
            @ApiImplicitParam(name = "email", required = false, value = "邮箱"),
            @ApiImplicitParam(name = "phone", required = false, value = "手机号"),
            @ApiImplicitParam(name = "address", required = false, value = "家庭地址"),
            @ApiImplicitParam(name = "faceUrl", required = false, value = "人脸照片"),
            @ApiImplicitParam(name = "status", required = false, value = "状态") })
    @PostMapping("/updateSysUser")
    public Result updateSysUser(@RequestParam Long id, String email, String phone, String address, String faceUrl,
                                StatusEnum status, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        userService.updateSysUser(id, email, phone, address, faceUrl, status, user);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "更新家政公司人员信息接口-家政公司端/系统端")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "用户id"),
            @ApiImplicitParam(name = "email", required = false, value = "邮箱"),
            @ApiImplicitParam(name = "phone", required = false, value = "手机号"),
            @ApiImplicitParam(name = "address", required = false, value = "家庭地址"),
            @ApiImplicitParam(name = "faceUrl", required = false, value = "人脸照片"),
            @ApiImplicitParam(name = "status", required = false, value = "状态"),
            @ApiImplicitParam(name = "labelId", required = false, value = "标签id") })
    @PostMapping("/updateCompanyUser")
    public Result updateCompanyUser(@RequestParam Long id, String email, String phone, String address, String faceUrl,
                                    StatusEnum status, Long labelId, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        userService.updateCompanyUser(id, email, phone, address, faceUrl, status, labelId, user);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "批量获取顾客信息接口-系统端")
    @ApiImplicitParams({ @ApiImplicitParam(name = "email", value = "邮箱"),
            @ApiImplicitParam(name = "name", value = "姓名"), @ApiImplicitParam(name = "sex", value = "性别"),
            @ApiImplicitParam(name = "status", value = "状态"), @ApiImplicitParam(name = "phone", value = "手机号"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @PostMapping("/listCustomer")
    public Result listCustomer(String email, String name, SexEnum sex, String phone, StatusEnum status, Integer pageNum,
                               Integer pageSize, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        Page<Customer> page = PageHelper.startPage(pageNum, pageSize);
        userService.listCustomer(email, name, sex, phone, status, user);
        return new Result(new PageVO<>(page.getTotal(), page.getResult()));
    }


    @ApiOperation(value = "批量获取系统人员信息接口-系统端")
    @ApiImplicitParams({ @ApiImplicitParam(name = "email", value = "邮箱"),
            @ApiImplicitParam(name = "name", value = "姓名"), @ApiImplicitParam(name = "sex", value = "性别"),
            @ApiImplicitParam(name = "status", value = "状态"), @ApiImplicitParam(name = "phone", value = "手机号"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @PostMapping("/listSysUser")
    public Result listSysUser(String email, String name, SexEnum sex, String phone, StatusEnum status, Integer pageNum,
                              Integer pageSize, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        Page<SysUser> page = PageHelper.startPage(pageNum, pageSize);
        userService.listSysUser(email, name, sex, phone, status, user);
        return new Result(new PageVO<>(page.getTotal(), page.getResult()));
    }


    @ApiOperation(value = "批量获取家政人员信息接口-家政公司或系统端")
    @ApiImplicitParams({ @ApiImplicitParam(name = "email", value = "邮箱"),
            @ApiImplicitParam(name = "name", value = "姓名"), @ApiImplicitParam(name = "sex", value = "性别"),
            @ApiImplicitParam(name = "status", value = "状态"), @ApiImplicitParam(name = "phone", value = "手机号"),
            @ApiImplicitParam(name = "companyId", value = "公司id"), @ApiImplicitParam(name = "type", value = "员工类型"),
            @ApiImplicitParam(name = "labelId", value = "服务标签id"), @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @PostMapping("/listCompanyUser")
    public Result listCompanyUser(String email, String name, SexEnum sex, String phone, StatusEnum status,
                                  Long companyId, CompanyUserTypeEnum type, Long labelId, Integer pageNum,
                                  Integer pageSize, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        Page<CompanyUser> page = PageHelper.startPage(pageNum, pageSize);
        userService.listCompanyUser(email, name, sex, phone, status, companyId, type, labelId, user);
        return new Result(new PageVO<>(page.getTotal(), page.getResult()));
    }


    @ApiOperation(value = "批量获取全部家政人员信息接口")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @GetMapping("/listAllCompanyUser")
    public Result listAllCompanyUser(CompanyUserTypeEnum type, Integer pageNum, Integer pageSize) {
        try (Page<CompanyUser> page = PageHelper.startPage(pageNum, pageSize)) {
            userService.listAllCompanyUser(type);
            return new Result(page.getResult());
        }
    }


    @ApiOperation(value = "添加系统用户接口-系统端")
    @ApiImplicitParams({ @ApiImplicitParam(name = "email", required = true, value = "邮箱"),
            @ApiImplicitParam(name = "password", required = true, value = "密码"),
            @ApiImplicitParam(name = "name", required = true, value = "姓名"),
            @ApiImplicitParam(name = "sex", required = true, value = "性别"),
            @ApiImplicitParam(name = "birth", required = true, value = "出生年月"),
            @ApiImplicitParam(name = "phone", required = true, value = "手机号"),
            @ApiImplicitParam(name = "address", required = true, value = "家庭地址"),
            @ApiImplicitParam(name = "faceUrl", required = true, value = "人脸照片") })
    @PostMapping("/addSysUser")
    public Result addSysUser(@RequestParam String email, @RequestParam String password, @RequestParam String name,
                             @RequestParam SexEnum sex, @RequestParam Date birth, @RequestParam String phone,
                             @RequestParam String address, @RequestParam String faceUrl, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        userService.addSysUser(email, password, name, sex, birth, phone, address, faceUrl, user);
        return Result.SUCCESS;

    }


    @ApiOperation(value = "添加家政人员接口-家政公司端")
    @ApiImplicitParams({ @ApiImplicitParam(name = "email", required = true, value = "邮箱"),
            @ApiImplicitParam(name = "password", required = true, value = "密码"),
            @ApiImplicitParam(name = "name", required = true, value = "姓名"),
            @ApiImplicitParam(name = "sex", required = true, value = "性别"),
            @ApiImplicitParam(name = "birth", required = true, value = "出生年月"),
            @ApiImplicitParam(name = "phone", required = true, value = "手机号"),
            @ApiImplicitParam(name = "address", required = true, value = "家庭地址"),
            @ApiImplicitParam(name = "faceUrl", required = true, value = "人脸照片"),
            @ApiImplicitParam(name = "type", required = true, value = "员工类型") })
    @PostMapping("/addCompanyUser")
    public Result addCompanyUser(@RequestParam String email, @RequestParam String password, @RequestParam String name,
                                 @RequestParam SexEnum sex, @RequestParam Date birth, @RequestParam String phone,
                                 @RequestParam String address, @RequestParam String faceUrl, @RequestParam Long labelId,
                                 @RequestParam BigDecimal salary, @RequestParam CompanyUserTypeEnum type,
                                 HttpServletRequest request) {
        Object user = request.getSession().getAttribute(SessionConstant.ACCOUNT);
        if (user instanceof CompanyUser) {
            CompanyUser companyUser = (CompanyUser) user;
            if (companyUser.getType() == CompanyUserTypeEnum.ADMIN) {
                userService.addCompanyUser(email, password, name, sex, birth, phone, address, faceUrl,
                        companyUser.getCompany().getId(), labelId, type, salary);
                return Result.SUCCESS;
            }
        }
        throw new ServiceException("非法操作,仅家政公司管理员能添加人员");
    }

    @ApiOperation(value = "顾客和家政员工总数量统计")
    @GetMapping("/countUser")
    public Result countUser(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        UserStatistic statistic = userService.countUser(user);
        return new Result(statistic);
    }
}
